var gulp = require('gulp');
var uglify = require('gulp-uglify')
var concat = require('gulp-concat')
var less = require('gulp-less')
var minifycss = require('gulp-clean-css')
var plumber = require('gulp-plumber');
var notify = require('gulp-notify');
var sourcemaps = require("gulp-sourcemaps"); 
var babel = require("gulp-babel");
var Autoprefixer = require('gulp-autoprefixer');//自动添加浏览器前缀
var changed = require('gulp-changed');

var postcss = require('gulp-postcss');   //postcss

var browserSync = require('browser-sync').create();  //刷新工具
var reload = browserSync.reload;

// var root = '我的坚果云/农商城/nong/';
// var root = 'swedenWebsite/WebRoot/';
// var root = '我的坚果云/云程/yun/';
 var root = '../layui-light-admin/';

/**
 * 处理less文件
**/
var processors=[Autoprefixer];
var browsers=[
  'last 3 versions',
  'ie >= 9',
  'ie_mob >= 10',
  'ff >= 30',
  'chrome >= 34',
  'safari >= 6',
  'opera >= 12.1',
  'ios >= 6',
  'android >= 4.4',
  'bb >= 10',
  'and_uc >= 9.9'
  ]

gulp.task('less-style', function() {
	gulp.src(root + 'dev/css/style.less')
	.pipe(plumber({errorHandler: notify.onError('Error: <%= error.message %>')}))
	.pipe(sourcemaps.init())
	.pipe(less())
	 .pipe(postcss(processors))
	.pipe(Autoprefixer({
		browsers:browsers,
	}))
	 .pipe(minifycss())
	.pipe(sourcemaps.write('./')) 
	.pipe(gulp.dest( root + 'static/css'))
	.pipe(browserSync.reload({stream:true}));
});

/**
 * 处理es6文件
**/
gulp.task("es-watch", function () { 
	return gulp.src(root + "dev/js/*.js") 
		.pipe(changed(root + 'static/js',{extension:'.js'}))
	.pipe(sourcemaps.init())
	.pipe(plumber({errorHandler: notify.onError('Error: <%= error.message %>')}))
	.pipe(babel()) 
	//.pipe(concat("main.js")) 
	// .pipe(uglify())
	.pipe(sourcemaps.write()) 
	.pipe(gulp.dest(root + "static/js"))
});
	
	
/**
 * broswer配置
**/
// 监听scss和html文件, 当文件发生变化后做些什么！
gulp.task('default', ['es-watch'], function () {
    browserSync.init({
        server: {
            baseDir: root
        }
    });
	gulp.watch(root + "dev/css/*", ['less-style']);
    gulp.watch(root + "dev/js/*.js", ['es-watch']);
    gulp.watch(root +"static/js/*.js").on("change", browserSync.reload);
    gulp.watch(root+ "page/*.html").on("change", reload);
});
// 代理

//gulp.task('browser-sync', function() {
//  browserSync.init({
//      proxy: "你的域名或IP"
//  });
//});
/**
 * broswer配置end
**/


// gulp.task('watch', function() {
// 	gulp.watch(root+'static/js/dev/*.js',['es-watch']);
// 	gulp.watch(root +'static/js/dist/*.js',['browser-sync']);
// 	gulp.watch(root +'page/*.html',['browser-sync'])
// });